<template>
  <div id="app">
    <router-view />
  </div>
</template>

<script>
export default {
  //绑定监听事件
  mounted() {
    window.addEventListener('beforeunload', e => this.beforeunloadHandler(e))
  },
  methods: {
    beforeunloadHandler(e) {
      console.log('关闭后=>为所欲为')
    }
  },
  //页面销毁前解除监听
  destroyed() {
    window.removeEventListener('beforeunload', e => this.beforeunloadHandler(e))
  },
  data() {
    return {
      beforeUnload: '',
      Handler: ''
    }
  },
  mounted() {
    window.addEventListener('beforeunload', e => this.beforeunloadHandler(e))
    window.addEventListener('unload', e => this.unloadHandler(e))
  },
  destroyed() {
    window.removeEventListener('beforeunload', e => this.beforeunloadHandler(e))
    window.removeEventListener('unload', e => this.unloadHandler(e))
  },
  ethods: {
    beforeunloadHandler() {
      this.beforeUnload = new Date().getTime();
    },
    unloadHandler(e) {
      this.Handler = new Date().getTime() - this.beforeUnload;
      //判断是窗口关闭还是刷新
      if (this.Handler <= 5) {
        console.log('为所欲为');
      }
    },
  }
}
</script>

<style lang="scss">
html,
body,
#app {
  width: 100%;
  height: 100%;
}

* {
  margin: 0;
  padding: 0;
}
</style>
